package com.hxzy.controller;

import cn.hutool.crypto.digest.BCrypt;
import com.alibaba.excel.EasyExcel;
import com.hxzy.common.annon.RequiredAdmin;
import com.hxzy.common.constans.Add;
import com.hxzy.common.constans.Update;
import com.hxzy.common.controller.BaseController;
import com.hxzy.common.dto.ChangeStatusDTO;
import com.hxzy.common.dto.ExistsDTO;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.util.WaimaiThreadLocal;
import com.hxzy.common.vo.R;
import com.hxzy.dto.DemoData;
import com.hxzy.dto.EmployeeSearchDTO;
import com.hxzy.entity.Employee;
import com.hxzy.service.EmployeeService;
import com.hxzy.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Api(tags = "员工API")
@RestController
@RequestMapping(value = "/api/employee")
public class EmployeeController extends BaseController {


    @Autowired
    private EmployeeService  employeeService;


    @RequiredAdmin
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/data")
    public R search(EmployeeSearchDTO employeeSearchDTO){

        //第一步，开启分页
        super.startPageHelper();
        //第二步，查询数据
        List<Employee> employeeList = this.employeeService.search(employeeSearchDTO);
        //第三步，把这个分页对象，组装成自己简单数据模型
        return super.pageToPageVO(employeeList);
    }

    @RequiredAdmin
    @ApiOperation(value = "excel导出")
    @GetMapping(value = "/excel")
    public void excel(EmployeeSearchDTO employeeSearchDTO, HttpServletResponse response) throws IOException {

        //第一步，查询数据
        List<Employee> employeeList = this.employeeService.search(employeeSearchDTO);

        //第二步，导出数据
        super.writeListToExcel(response,Employee.class,employeeList);

    }

    @RequiredAdmin
    @ApiOperation(value = "新增")
    @PostMapping
    public R add(@RequestBody @Validated(value = {Add.class}) Employee employee){

        //验证用户名不允许重复
        int count = this.existsService.loginNameExists(employee.getLoginName());
        if(count>0){
            return R.build(AckCode.USER_HAS_ACCOUNT);
        }

        //密码变为md5
        employee.setLoginPwd(BCrypt.hashpw(employee.getLoginPwd()));

        
        //调用新增方法
        boolean result = this.employeeService.save(employee);

        return super.toR(result);
    }


    @RequiredAdmin
    @ApiOperation(value = "修改")
    @PutMapping
    public R update(@RequestBody @Validated(value = {Update.class}) Employee employee){

        //验证账户被修改过没有
        Employee db = this.employeeService.selectByPrimaryKey(employee.getId());
        if(db==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        if(!employee.getLoginName().equals(db.getLoginName())){
            return R.build(AckCode.ACCOUNT_NAME_NOT_MODIFY);
        }

        //自己赋值
        db.setStatus(employee.getStatus());
        db.setGender(employee.getGender());
        db.setAvatar(employee.getAvatar());


        boolean result = this.employeeService.updateById(db);

        return super.toR(result);
    }

    @RequiredAdmin
    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R findById(@PathVariable(value = "id") Long id){


        Employee employee = this.employeeService.selectByPrimaryKey(id);
        if(employee==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        return R.okHasData(employee);
    }

    @RequiredAdmin
    @ApiOperation(value = "禁用账户")
    @DeleteMapping
    public R removeByIds(Long[] ids){
       int count=this.employeeService.removeByIds(ids);
       return super.toR(count);
    }

    @RequiredAdmin
    @ApiOperation(value = "改变状态")
    @PutMapping(value = "/status")
    public R changeStatus( @RequestBody @Valid ChangeStatusDTO changeStatusDTO){
        int count=this.employeeService.updateStatus(changeStatusDTO);
        return super.toR(count);
    }
}
